package Model;

import java.awt.Point;

import map.Cell;

public class CrossRoads {
	private Point a;
	private boolean blockA;
	private Point b;
	private boolean blockB;
	private Point c;
	private boolean blockC;
	private Point d;
	private boolean blockD;
	private Point aDownEntry;
	private Point aLeftExit;
	private Point bUpExit;
	private Point bLeftEntry;
	private Point cRightEntry;
	private Point cDownExit;
	private Point dRightExit;
	private Point dUpEntry;

//	private boolean aLeftExitBlock;
//	private boolean bUpExitBlock;
//	private boolean cDownExitBlock;
//	private boolean dRightExitBlock;
	
	
	private boolean aDownEntryBlock;
	private boolean bLeftEntryBlock;
	private boolean cRightEntryBlock;
	private boolean dUpEntryBlock;
	private boolean isBorder = false;
	public CrossRoads(Point a, Point b, Point c, Point d, boolean isBorder) {
		super();
		this.a = a;
		this.b = b;
		this.c = c;
		this.d = d;
		this.blockA = true;
		this.blockB = true;
		this.blockC = true;
		this.blockD = true;
		this.isBorder = isBorder;
		aDownEntry = new Point(this.getA().x - 1, this.getA().y);
		aLeftExit = new Point(this.getA().x, this.getA().y - 1);

		bUpExit = new Point(this.getB().x - 1, this.getB().y);
		bLeftEntry = new Point(this.getB().x, this.getB().y + 1);

		cRightEntry = new Point(this.getC().x, this.getC().y - 1);
		cDownExit = new Point(this.getC().x + 1, this.getC().y);

		dRightExit = new Point(this.getD().x, this.getD().y + 1);
		dUpEntry = new Point(this.getD().x + 1, this.getD().y);

//		aLeftExitBlock = false;
//		bUpExitBlock = false;
//		cDownExitBlock = false;
//		dRightExitBlock = false;
		
		aDownEntryBlock = true;
		bLeftEntryBlock = true;
		cRightEntryBlock = true;
		dUpEntryBlock = true;
	}

	public boolean isBorder() {
		return isBorder;
	}

	public boolean isaDownEntryBlock() {
		return aDownEntryBlock;
	}

	public void setaDownEntryBlock(boolean aDownEntryBlock) {
		this.aDownEntryBlock = aDownEntryBlock;
	}

	public boolean isbLeftEntryBlock() {
		return bLeftEntryBlock;
	}

	public void setbLeftEntryBlock(boolean bLeftEntryBlock) {
		this.bLeftEntryBlock = bLeftEntryBlock;
	}

	public boolean iscRightEntryBlock() {
		return cRightEntryBlock;
	}

	public void setcRightEntryBlock(boolean cRightEntryBlock) {
		this.cRightEntryBlock = cRightEntryBlock;
	}

	public boolean isdUpEntryBlock() {
		return dUpEntryBlock;
	}

	public void setdUpEntryBlock(boolean dUpEntryBlock) {
		this.dUpEntryBlock = dUpEntryBlock;
	}

//	public boolean isaLeftExitBlock() {
//		return aLeftExitBlock;
//	}
//
//	public void setaLeftExitBlock(boolean aLeftExitBlock) {
//		this.aLeftExitBlock = aLeftExitBlock;
//	}
//
//	public boolean isbUpExitBlock() {
//		return bUpExitBlock;
//	}
//
//	public void setbUpExitBlock(boolean bUpExitBlock) {
//		this.bUpExitBlock = bUpExitBlock;
//	}
//
//	public boolean iscDownExitBlock() {
//		return cDownExitBlock;
//	}
//
//	public void setcDownExitBlock(boolean cDownExitBlock) {
//		this.cDownExitBlock = cDownExitBlock;
//	}
//
//	public boolean isdRightExitBlock() {
//		return dRightExitBlock;
//	}
//
//	public void setdRightExitBlock(boolean dRightExitBlock) {
//		this.dRightExitBlock = dRightExitBlock;
//	}

	public Point getaDownEntry() {
		return aDownEntry;
	}

	public void setaDownEntry(Point aDownEntry) {
		this.aDownEntry = aDownEntry;
	}

	public Point getaLeftExit() {
		return aLeftExit;
	}

	public void setaLeftExit(Point aLeftExit) {
		this.aLeftExit = aLeftExit;
	}

	public Point getbUpExit() {
		return bUpExit;
	}

	public void setbUpExit(Point bUpExit) {
		this.bUpExit = bUpExit;
	}

	public Point getbLeftEntry() {
		return bLeftEntry;
	}

	public void setbLeftEntry(Point bLeftEntry) {
		this.bLeftEntry = bLeftEntry;
	}

	public Point getcRightEntry() {
		return cRightEntry;
	}

	public void setcRightEntry(Point cRightEntry) {
		this.cRightEntry = cRightEntry;
	}

	public Point getcDownExit() {
		return cDownExit;
	}

	public void setcDownExit(Point cDownExit) {
		this.cDownExit = cDownExit;
	}

	public Point getdRightExit() {
		return dRightExit;
	}

	public void setdRightExit(Point dRightExit) {
		this.dRightExit = dRightExit;
	}

	public Point getdUpEntry() {
		return dUpEntry;
	}

	public void setdUpEntry(Point dUpEntry) {
		this.dUpEntry = dUpEntry;
	}

	public Point getA() {
		return a;
	}

	public Point getB() {
		return b;
	}

	public Point getC() {
		return c;
	}

	public Point getD() {
		return d;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((a == null) ? 0 : a.hashCode());
		result = prime * result + ((b == null) ? 0 : b.hashCode());
		result = prime * result + ((c == null) ? 0 : c.hashCode());
		result = prime * result + ((d == null) ? 0 : d.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		CrossRoads other = (CrossRoads) obj;
		if (a == null) {
			if (other.a != null)
				return false;
		} else if (!a.equals(other.a))
			return false;
		if (b == null) {
			if (other.b != null)
				return false;
		} else if (!b.equals(other.b))
			return false;
		if (c == null) {
			if (other.c != null)
				return false;
		} else if (!c.equals(other.c))
			return false;
		if (d == null) {
			if (other.d != null)
				return false;
		} else if (!d.equals(other.d))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "CrossRoads [a=" + a + "]";
	}

	public void setBlockA(boolean blockA) {
		this.blockA = blockA;
	}

	public void setBlockB(boolean blockB) {
		this.blockB = blockB;
	}

	public void setBlockC(boolean blockC) {
		this.blockC = blockC;
	}

	public void setBlockD(boolean blockD) {
		this.blockD = blockD;
	}

	public void setA(Point a) {
		this.a = a;
	}

	public void setB(Point b) {
		this.b = b;
	}

	public void setC(Point c) {
		this.c = c;
	}

	public void setD(Point d) {
		this.d = d;
	}

	public boolean isClosed(Point curr) {
		if (curr.equals(a))
			return blockA;
		if (curr.equals(b))
			return blockB;
		if (curr.equals(c))
			return blockC;
		if (curr.equals(d))
			return blockD;
		return false;
	}

	public void setClosed(Point curr, boolean closed) {
		if (curr.equals(a))
			blockA = closed;
		if (curr.equals(b))
			blockB = closed;
		if (curr.equals(c))
			blockC = closed;
		if (curr.equals(d))
			blockD = closed;
	}

	public boolean isFree(Cell[][] map) {
		if (map[a.x][a.y].isOccupied()) {
			return false;
		}
		if (map[b.x][b.y].isOccupied()) {
			return false;
		}
		if (map[c.x][c.y].isOccupied()) {
			return false;
		}
		if (map[d.x][d.y].isOccupied()) {
			return false;
		}
		return true;
	}

	public int getNumCarInCrossRoads(Cell[][] map) {
		int num = 0;
		if (map[a.x][a.y].isOccupied()) {
			num++;
		}
		if (map[b.x][b.y].isOccupied()) {
			num++;
		}
		if (map[c.x][c.y].isOccupied()) {
			num++;
		}
		if (map[d.x][d.y].isOccupied()) {
			num++;
		}
		return num;
	}

	public boolean isClosedEntry(Point point) {
		if (point.equals(a)) {
			return aDownEntryBlock;
		}
		if (point.equals(b)) {
			return bLeftEntryBlock;
		}
		if (point.equals(c)) {
			return cRightEntryBlock;
		}
		if (point.equals(d)) {
			return dUpEntryBlock;
		}
		return false;
	}

//	public boolean isClosedExit(Point point) {
//		if (point.equals(a)) {
//			return aLeftExitBlock;
//		}
//		if (point.equals(b)) {
//			return bUpExitBlock;
//		}
//		if (point.equals(c)) {
//			return cDownExitBlock;
//		}
//		if (point.equals(d)) {
//			return dRightExitBlock;
//		}
//		return false;
//	}
}
